﻿Imports Sunts.HyperV.Common

Public Class frmMailServer

    Private Sub chkAuth_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSMTPAuthentication.CheckedChanged
        If chkSMTPAuthentication.Checked = True Then
            txtSMTPUsername.Enabled = True
            txtSMTPPassword.Enabled = True
        Else
            With txtSMTPUsername
                .Enabled = False
                .Text = String.Empty
            End With
            With txtSMTPPassword
                .Enabled = False
                .Text = String.Empty
            End With
        End If
    End Sub

    Private Sub EmailSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            For Each MailServer As MailServer In MailServer.SelectAll(My.Settings.ConnectionString)
                txtMailServer.Text = MailServer.MailServer
                txtPort.Text = CStr(MailServer.Port)
                txtSenderName.Text = MailServer.SenderName
                txtSenderEmailAddress.Text = MailServer.SenderEmailAddress
                chkSMTPAuthentication.Checked = MailServer.SMTPAuthentication
                txtSMTPUsername.Text = MailServer.SMTPUsername
                txtSMTPPassword.Text = MailServer.SMTPPassword
                Exit Sub
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnSaveServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveServer.Click
        Try
            Dim MailServer As New MailServer
            Dim MailServerCollection As New MailServerCollection

            MailServerCollection = MailServer.SelectAll(My.Settings.ConnectionString)

            For Each ctrl As Control In Me.GroupBox4.Controls
                If TypeOf ctrl Is TextBox Then
                    If ctrl.Enabled = True And ctrl.Text = String.Empty Then
                        MsgBox("Could not add the server" & ControlChars.CrLf & _
                                    "Ensure that you have filled in all of the information above", CType(vbCritical + vbOKOnly, MsgBoxStyle))
                        Exit Sub
                    End If
                End If
            Next

            MailServer.MailServer = txtMailServer.Text
            MailServer.Port = CInt(txtPort.Text)
            MailServer.SenderName = txtSenderName.Text
            MailServer.SenderEmailAddress = txtSenderEmailAddress.Text
            MailServer.SMTPAuthentication = chkSMTPAuthentication.Checked
            MailServer.SMTPUsername = txtSMTPUsername.Text
            MailServer.SMTPPassword = txtSMTPPassword.Text

            If MailServerCollection.Count > 0 Then
                MailServer.MailServerID = MailServerCollection.Item(0).MailServerID
                MailServer.Update(My.Settings.ConnectionString)
            Else
                MailServer.Insert(My.Settings.ConnectionString)
            End If

            MessageBox.Show("Server details saved successfully")

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

End Class